<?php

/**
 * 十进制数转换成62进制
 * http://blog.csdn.net/zxjiayou1314/article/details/52454370
 * @param integer $num
 * @return string
 */
function from10_to62($num) {
    $to = 62;
    $dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $ret = '';
    do {
        $ret = $dict[bcmod($num, $to)] . $ret;
        $num = bcdiv($num, $to);
    } while ($num > 0);
    return $ret;
}

/**
 * 62进制数转换成十进制数
 *
 * @param string $num
 * @return string
 */
function from62_to10($num) {
    $from = 62;
    $num = strval($num);
    $dict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $len = strlen($num);
    $dec = 0;
    for($i = 0; $i < $len; $i++) {
        $pos = strpos($dict, $num[$i]);
        $dec = bcadd(bcmul(bcpow($from, $len - $i - 1), $pos), $dec);
    }
    return $dec;
}

/**
 * 获取当前登录的管理员ID
 * @return int
 */
function get_admin_id()
{
    return session('ADMIN_ID');
}

/**
 * 获取当前登录的管理员用户名
 * @return string
 */
function get_admin_account()
{
    return session('ADMIN_USERNAME');
}


/**
 * 判断是否是超级管理员
 * @return boolean
 */
function is_super_admin()
{
    return session('ADMIN_ID') == 1;
}

/**
 * 判断管理员是否登录
 * @return boolean
 */
function is_admin_login()
{
    $sessionAdmin = session('ADMIN_ID');
    return !empty($sessionAdmin);
}

/**
 * 密码加密方法
 * @param string $pw 要加密的原始密码
 * @return string
 */
function encrypt_password($pw)
{
    $authCode = 'act.wapui.com';
    $result = md5(md5($authCode . $pw));
    return $result;
}

/**
 * 密码比较方法,所有涉及密码比较的地方都用这个方法
 * @param string $password 要比较的密码
 * @param string $passwordInDb 数据库保存的已经加密过的密码
 * @return boolean 密码相同，返回true
 */
function compare_password($password, $passwordInDb)
{
    return encrypt_password($password) === $passwordInDb;
}

/**
 * 随机字符串生成
 * @param int $len 生成的字符串长度
 * @return string
 */
function get_random_string($len = 6)
{
    $chars    = [
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
        "3", "4", "5", "6", "7", "8", "9"
    ];
    $charsLen = count($chars) - 1;
    shuffle($chars);    // 将数组打乱
    $output = "";
    for ($i = 0; $i < $len; $i++) {
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
}


/** 
 * 检查管理员用户名是否符合规定 
 * 字母,数字,或者下划线，5-10
 * 
 * @param STRING $username 要检查的用户名 
 * @return TRUE or FALSE 
 */
function check_admin_username($username) { 
    $strlen = strlen($username); 
    if (!preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)) { 
        return false; 
    }elseif (10 < $strlen || $strlen < 5) { 
        return false; 
    }
    return true; 
}

/** 
 * 检查管理员密码是否符合规定 
 * 8-20位字符
 * 
 * @param STRING $password 要检查的密码
 * @return TRUE or FALSE 
 */
function check_admin_password($password) { 
    if (!preg_match("/^[\w-!@#%^&()\?\*\.\$]{8,20}$/", $password)) { 
        return false; 
    }
    return true; 
}



?>